package com.davidk.survey.client.dataviewer;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

public class DataView extends VLayout implements DataViewInterface {

	@Override
	public ListGrid getGrid() {
		return grid;
	}

	ListGrid grid = new ListGrid();
	DynamicForm form = new DynamicForm();
	private IButton saveButton;

	public DataView() {
		final SectionStack sectionStack = new SectionStack();
		sectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);

		grid.setHeight("50%");
		grid.setAutoFetchData(true);
		grid.setShowFilterEditor(true);
		grid.addCellClickHandler(new CellClickHandler() {
			@Override
			public void onCellClick(CellClickEvent event) {
				form.editSelectedData(grid);
			}
		});

		SectionStackSection gridSect = new SectionStackSection("Item List");
		gridSect.setExpanded(true);
		gridSect.addItem(grid);
		ImgButton addButton = new ImgButton();
		addButton.setSrc("[SKIN]actions/add.png");
		addButton.setSize(16);
		addButton.setShowFocused(false);
		addButton.setShowRollOver(false);
		addButton.setShowDown(false);
		addButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				form.editNewRecord();
			}
		});
		gridSect.setControls(addButton);

		final TabSet tabSet = new TabSet();
		tabSet.setTabBarPosition(Side.TOP);
		tabSet.setHeight("50%");

		Tab tTab1 = new Tab("Item Viewer");
		saveButton = new IButton("Save");
		saveButton.setLayoutAlign(Alignment.CENTER);
		saveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				form.saveData();
			}
		});
		form.setUseAllDataSourceFields(true);
		form.disable();
		VLayout editorLayout = new VLayout(5);
		editorLayout.addMember(form);
		editorLayout.addMember(saveButton);
		tTab1.setPane(editorLayout);
		tabSet.addTab(tTab1);

		SectionStackSection detailSect = new SectionStackSection("Item Detail");
		detailSect.setExpanded(true);
		detailSect.addItem(tabSet);

		sectionStack.setSections(gridSect, detailSect);
		addMember(sectionStack);
	}

	@Override
	public void setDataSource(DataSource ds) {
		System.out.println("DataView : Setting Datasource : parent = "
				+ this.getParentElement());
		grid.setDataSource(ds);
		form.setDataSource(ds);
		grid.fetchData();
		System.out.println("DataView : Done Setting Datasource : " + ds);
	}

	/**
	 * Create a new view each time to prevent ISC_GRID errors when setting datasource
	 */
	@Override
	public DataView getView(DataSource ds) {
		DataView ret = new DataView();
		ret.setDataSource(ds);
		return ret;
	}

}
